using Microsoft.Extensions.DependencyInjection;
using ZeroBlog.Application.FlashArticles.Dtos.In;
using ZeroBlog.Application.FlashArticles.Dtos.Out;
using ZeroBlog.Application.FlashArticles.Dtos.Query;
using ZeroBlog.Core.Domains.Blog;
using ZeroBlog.Core.DomainServices.FlashArticles;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Extend.Mapper.Extensions;
using ZeroSum.Models;

namespace ZeroBlog.Application.FlashArticles;

[Register(ServiceLifetime.Scoped)]
public class FlashArticleApplicationService : IFlashArticleApplicationService
{
    private readonly IFlashArticleDomainService _domainService;

    public FlashArticleApplicationService(IFlashArticleDomainService domainService)
    {
        _domainService = domainService;
    }

    public async Task AddAsync(AddFlashArticleDto dto)
    {
        var domain = dto.MapTo<FlashArticle>();
        await _domainService.AddAsync(domain);
    }

    public async Task DeleteAsync(DeleteFlashArticleDto dto)
    {
        await _domainService.DeleteAsync(dto.Ids);
    }

    public async Task UpdateAsync(UpdateFlashArticleDto dto)
    {
        var domain = dto.MapTo<FlashArticle>();
        await _domainService.UpdateAsync(domain);
    }

    public async Task<DetailFlashArticleDto> Detail(DetailFlashArticleQuery query)
    {
        var domain = await _domainService.GetAsync(x => x.Id == query.Id);
        return domain.MapTo<DetailFlashArticleDto>();
    }

    public async Task<PagedResponseModel<PageFlashArticleDto>> Page(PageFlashArticleQuery query)
    {
        var (domains, count) = await _domainService.Query(query.Keyword, query.PageNumber, query.PageSize);
        var list = domains.MapTo<List<PageFlashArticleDto>>();
        return new PagedResponseModel<PageFlashArticleDto>(query.PageNumber, query.PageSize, count, list);
    }
}